package DataStructuresTest;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

public class AlgorithmTest {
    /**
     * 以O(n)时间复杂度查找出出现次数最多的元素
     */
    public static void main(String[] args) {
//        int[] nums = {2,3,1,5,4,2,3,2,1};
        int[] nums = {2,2,1,5,4,2,3,3};

        System.out.println(findMostFrequencyElement(nums));
    }

    public static int findMostFrequencyElement(int[] nums) {
            int result = 0;
            int count = 0;
            for (int i = 0;i < nums.length; i++) {
                if (i == 0){
                    result = nums[i];
                    count++;
                }else {
                    if (count == 0){
                        result = nums[i];
                    }else if (result != nums[i]){
                        count--;
                    }else {
                        count++;
                    }
                }
            }
            return result;
        }
}
